﻿Imports LRS.BLL
Imports LRS.DTO
Imports System.Windows.Forms

Public Class ucProducts
    Private ProductID As Integer
    Public Function ucValidatePro() As Boolean

        If txtProductName.Text.Length = 0 Or txtQuantity.Text.Length = 0 Or txtRam.Text.Length = 0 Or txtCPU.Text.Length = 0 Or txtHDD.Text.Length = 0 Or txtGraphics.Text.Length = 0 Or txtScreen.Text.Length = 0 Or rtxtOtherInfo.Text.Length = 0 Or txtRentingPrice.Text.Length = 0 Then
            MessageBox.Show("Bạn vui lòng nhập đầy đủ thông tin!")
            txtProductName.Focus()
            Return False
        End If
        
        If txtQuantity.Text Like "[0-9]*" Then
            Return True
        Else
            MessageBox.Show("Số lượng không đúng định dạng!")
            txtQuantity.Text = ""
            txtQuantity.Focus()
            Return False
        End If
        If txtRentingPrice.Text Like "[0-9]*" Then
            Return True
        Else
            MessageBox.Show("Giá cho thuê không đúng định dạng")
            txtRentingPrice.Text = ""
            txtRentingPrice.Focus()
            Return False
        End If
        If txtSellingPrice.Text Like "[0-9]*" Then
            Return True
        Else
            MessageBox.Show("Giá cho thuê không đúng định dạng")
            txtSellingPrice.Text = ""
            txtSellingPrice.Focus()
            Return False
        End If
        Return True
    End Function

    Private Sub cmbManuId_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbManuId.SelectedIndexChanged

    End Sub

    Private Sub cmbSupplierID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbSupplierID.SelectedIndexChanged

    End Sub

    Private Sub ucProducts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbManuId.DataSource = bManufacturer.GetManufacturers()
        cmbManuId.DisplayMember = "ManufacturerName"
        cmbManuId.ValueMember = "ManufacturerID"

        cmbSupplierID.DataSource = bSupplier.GetSuppliers()
        cmbSupplierID.DisplayMember = "SupplierName"
        cmbSupplierID.ValueMember = "SupplierID"
    End Sub
    Public Sub ClearText()
        For Each c As Control In grpProducts.Controls
            If (TypeOf c Is TextBox Or TypeOf c Is RichTextBox) Then
                c.ResetText()
            End If
        Next
    End Sub
    Public Sub SetProduct(ByVal product As eProduct)
        ProductID = product.ProductID
        txtCPU.Text = product.CPU
        txtProductName.Text = product.ProductName
        txtRam.Text = product.RAM
        txtCPU.Text = product.CPU
        txtGraphics.Text = product.Graphics
        txtQuantity.Text = product.Quantity
        txtScreen.Text = product.Screen
        txtHDD.Text = product.HDD
        txtRentingPrice.Text = product.RentingPrice.ToString("N")
        txtSellingPrice.Text = product.SellingPrice.ToString("N")
        rtxtOtherInfo.Text = product.OtherInfo
        cmbManuId.SelectedValue = product.ManufacturerID
        cmbSupplierID.SelectedValue = product.SupplierID
    End Sub

    Public Function GetProduct() As eProduct
        If (ucValidatePro()) Then
            Dim product As eProduct = New eProduct()
            product.ProductName = txtProductName.Text
            product.CPU = txtCPU.Text
            product.HDD = txtHDD.Text
            product.RAM = txtRam.Text
            product.Graphics = txtGraphics.Text
            product.OtherInfo = rtxtOtherInfo.Text
            product.Quantity = Int32.Parse(txtQuantity.Text)
            product.RentingPrice = Single.Parse(txtRentingPrice.Text)
            product.SellingPrice = Single.Parse(txtSellingPrice.Text)
            product.Screen = txtScreen.Text
            product.ManufacturerID = cmbManuId.SelectedValue
            product.SupplierID = cmbSupplierID.SelectedValue
            product.ProductID = ProductID
            Return product
        Else
            Return Nothing
        End If
    End Function
End Class
